/**
 * Copyright(c) 2014 Xunton Solutions
 *
 * History:
 *   15-8-13 上午11:33 Created by dwzhang
 */
package com.jade.framework.base.util;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 分页的返回结果。
 *
 * @author <a href="mailto:dwzhang@xunton.com">dwzhang</a> 
 * @version 1.0 15-8-13 上午11:33
 */
public class PagingResult
{
    private int status = 200;
    private long total;
    private Object[] rows;
    private Map<String, Object> params;
    private int offset;
    private int pageSize;
    //客户端带的参数，直接返回。Datatables需要使用
    private String callback;

    public static <T> PagingResult getResult(PagingList<T> list)
    {
        return getResult(list, list.getTotalCount());
    }

    public static <T> PagingResult getResult(List<T> list, long totalCount)
    {
        PagingResult result = new PagingResult();
        result.setRows(list);
        result.setTotal(totalCount);
        return result;
    }
    public static <T> PagingResult getResult(List<T> list, long totalCount,int offset,int pageSize)
    {
        PagingResult result = new PagingResult();
        result.setRows(list);
        result.setTotal(totalCount);
        result.setOffset(offset);
        result.setPageSize(pageSize);
        return result;
    }
    public int getStatus()
    {
        return status;
    }

    public void setStatus(int status)
    {
        this.status = status;
    }

    public long getTotal()
    {
        return total;
    }

    public void setTotal(long total)
    {
        this.total = total;
    }

    public Object[] getRows()
    {
        return rows;
    }

    public void setRows(Object[] rows)
    {
        this.rows = rows;
    }

    public <T> void setRows(List<T> data)
    {
        this.rows = new Object[data.size()];
        for (int i = 0; i < data.size(); i++) {
            this.rows[i] = data.get(i);
        }
    }

    public Map<String, Object> getParams()
    {
        return params;
    }

    public void setParams(Map<String, Object> params)
    {
        this.params = params;
    }

    public void setParam(String key, Object value)
    {
        if (params == null) {
            params = new HashMap<String, Object>();
        }
        params.put(key, value);
    }

    public int getOffset()
    {
        return offset;
    }

    public void setOffset(int offset)
    {
        this.offset = offset;
    }

    public int getPageSize()
    {
        return pageSize;
    }

    public void setPageSize(int pageSize)
    {
        this.pageSize = pageSize;
    }

    public String getCallback()
    {
        return callback;
    }

    public void setCallback(String callback)
    {
        this.callback = callback;
    }
}
